package com.lungle.warmdiary.app.api;

import com.lungle.warmdiary.app.AppConstant;
import com.lungle.warmdiary.app.AppContext;
import com.lungle.warmdiary.app.api.convert.STGsonConverterFactory;

import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public enum RetrofitClient {
    INSTANCE;

    private volatile Retrofit retrofit;
    private final Retrofit.Builder mBuilder;
    private volatile String baseUrl;

    RetrofitClient() {
        baseUrl = AppConstant.API_URL;
        mBuilder = new Retrofit.Builder().addConverterFactory(STGsonConverterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create());

    }

    public Retrofit getRetrofit(boolean hasToken) {

        if (AppConstant.OPEN) {
            if (AppContext.getInstance().openCustom) {
                baseUrl = AppContext.getInstance().getProperty(AppContext.CUSTOM);
            } else if (AppConstant.OPEN && AppContext.getInstance().openRelease) {
                baseUrl = AppConstant.API_DEBUG_URL;
            }
        }
        retrofit = mBuilder.baseUrl(baseUrl).client(OKHttpFactory.INSTANCE.getOkHttpClient(hasToken)).build();
        return retrofit;
    }
}